<template>
  <el-form-item :class="getClz()" :label="opt.text" :prop="opt.name" v-if="!opt.hide"  v-bind="opt.itemProps">
    <el-cascader
      style="width: 100%;"
      :placeholder="opt.props.disabled?'':opt.placeholder"
      v-model="value"
      v-bind="opt.props"
      :clearable="true"
      :options="opt.options"
      @change="handleChange">
    </el-cascader>
  </el-form-item>
</template>

<script>
  import extend from '../extend';
  let Log = ()=>{};

  export default {
    name: "FormSelect",
    mixins:[extend],
    dicts:[],
    props: {
      column: {
        type:[Object,Array],
        default:{}
      },
      data:{
        type:[Object,Array],
        default:{}
      }
    },
    data(){
      return {
        value:[],
        opt: {
          name:'',
          text:'',
          prop:'',
          itemProps:{
          },
          props:{
            disabled:false,
            filterable:true
          },
          hide:false,
          placeholder:'',
          options:[]
        }
      }
    },
    mounted(){
      this.initOptions();
      this.onChange = this.column.onChange||Log;
      this.onOptions = this.column.onOptions||Log;
      //
      this.$watch(`data.${this.column.name}`,(val)=>{
        this.value = val.split(',')
      })
    },
    methods:{
      setValue(val = ''){
        this.data[this.opt.name] = val;
      },
      setValueMap(val){
        let opt = this.opt, values = opt.props.multiple ? val : [val];
        this.onChange(this.data,this.column);
      },
      handleChange(){
        let str = this.value.join(',');
        this.setValue(str)
        this.setValueMap(str)
      }
    }
  }
</script>
